import * as tencentcloud from "tencentcloud-sdk-nodejs";
const OcrClient = tencentcloud.ocr.v20181119.Client;

const clientConfig = {
  credential: {
    // 'AKIDc5KL8iaA2g4C5lwWWHo5VjkbGjy2LX0I',
    // 'HQM5bv2wkNp34ta5NWVJtFOL6f291dyC'
    secretId: "AKIDNYklX3MUyIPJy3kmAV4MwpUKxRehLbid",
    secretKey: "rWxBDPO2QXFoyBDaicLOJzShogLYVIsl",
  },
  region: "ap-guangzhou",
  profile: {
    httpProfile: {
      endpoint: "ocr.tencentcloudapi.com",
    },
  },
};
const client = new OcrClient(clientConfig);

function getCaptcha(base64) {
  const params = { ImageBase64: base64 };

  const promise = new Promise((resolve, reject) => {
    client.GeneralBasicOCR(params).then(
      (data) => {
        const { TextDetections = [] } = data;
        const [{ DetectedText }] = TextDetections;
        resolve(DetectedText);
      },
      (err) => {
        // reject(err);
        reject('未识别出验证码')
      }
    );
  });

  return promise;
}

export { getCaptcha };
